package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.location.LocationExtend;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.location.LocationMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CHANGE_CITY, desc = "修改城市")
public class ChangeCityCmd extends AbstractCommand {
    private String city;
    private int id;
    private LocationExtend locationExtend;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PlayerProto.ChangeCityReqMsg reqMsg = PlayerProto.ChangeCityReqMsg.parseFrom(packet.getBytes());
        city = reqMsg.getCity();
        id = reqMsg.getId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(StringUtils.isNullOrEmpty(city) || id == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        locationExtend = LocationMgr.findLocationExtend(id, city);
        if (locationExtend == null) {
            ret = GameErrorCode.E_CITY_NOT_FIND_ERROR;
            return;
        }

        // 修改次数大于0,不允许修改
        long num = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.ChangCityNum);
        if(num < GameConfig.CHANG_LOCATION_NUM){
            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.ChangCityNum, num + 1);
        } else if (player.getModule(CurrencyModule.class).removeCurrency(new Property(eItemId.CHANGE_CITY_ITEM.getValue(), BigInteger.ONE), eLogMoneyType.Bag, eLogMoneyType.ChangeCityByUseItem)) {

        } else {
            ret = GameErrorCode.E_CITY_CHANGE_NUM_OVER;
            return;
        }

        player.getModule(PlayerModule.class).setPosition(locationExtend.getWorld(), locationExtend.getContinent(), locationExtend.getCountry(), locationExtend.getProvince(), city);

    }

    @Override
    public void sendData(GamePlayer player) {
        PlayerProto.ChangeCityRespMsg.Builder respMsg = PlayerProto.ChangeCityRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setWorld(player.getUserInfo().getWorld());
        respMsg.setContinent(player.getUserInfo().getContinent());
        respMsg.setCountry(player.getUserInfo().getCountry());
        respMsg.setProvince(player.getUserInfo().getProvince());
        respMsg.setCity(player.getUserInfo().getCity());
        player.sendPacket(Protocol.U_CHANGE_CITY, respMsg);
    }
}
